/*
 * Copyright 2012 Mirella Esther da Silva
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mirella.parser.csv;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.mirella.alerts.Alert;
import com.mirella.parser.Parser;


/**
 * CSV files parser. Implements @interface Parser.
 */

public class CSVParser implements Parser {

	private String buffer;
	private Alert currentAlert;

	@Override
	public List<Alert> parse(InputStream input) {

		BufferedReader in = new BufferedReader(new InputStreamReader(input));
		List<Alert> alerts = new ArrayList<Alert>();
		try {
			String line = in.readLine();
			if(line != null) {
				String [] types = line.split(",");
				while((line = in.readLine()) != null) {
					buffer = "";
					currentAlert = new Alert();
					String [] tokens = line.split(",");
					for (int i = 0; i < tokens.length && i < types.length; i++) {
						transform(types[i], tokens[i]);
					}
					currentAlert.setDescription(buffer);
					alerts.add(currentAlert);
				}
			}
		} catch (IOException e) {
			Log.e("CSVParser", "error with csv file", e); 
		}
		return alerts;
	}

	//hail	  - Time,Size,Location,County,State,Lat,Lon,Comments
	//tornado - Time,F-Scale,Location,County,State,Lat,Lon,Comments
	//wind	  - Time,Speed,Location,County,State,Lat,Lon,Comments

	private void transform(String type, String token) {

		if (currentAlert != null) {
			if ("Time".equals(type) 
					&& token.length() == 4) {
				this.buffer += "Time: "+token.substring(0, 2)+":"+token.substring(2, 4)+"\n";
			} else if ("Size".equals(type)) {
				this.buffer += "Size: "+token+"\"\n";
				currentAlert.setTitle("Hail");
			} else if ("Location".equals(type)) {
				this.buffer += "Location: "+token+"\n";
			} else if ("County".equals(type)) {
				this.buffer += "County: "+token+"\n";
			} else if ("State".equals(type)) {
				this.buffer += "State: "+token+"\n";
			} else if ("Lat".equals(type)) {
				this.buffer += "Lat: "+token+"\n";
				currentAlert.setLat((int)(Double.valueOf(token)*1E6));
			} else if ("Lon".equals(type)) {
				this.buffer += "Lon: "+token+"\n";
				currentAlert.setLng((int)(Double.valueOf(token)*1E6));
			} else if ("Comments".equals(type)) {
				this.buffer += "\n"+token+"\n";
			} else if ("F-Scale".equals(type)) {
				this.buffer += "F-Scale: "+token+"\n";
				currentAlert.setTitle("Tornado");
			} else if ("Speed".equals(type)) {
				this.buffer += "Speed: "+token+" miles\n";
				currentAlert.setTitle("Wind");
			}  

		}

	}

}
